<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-2.1.1.min.js"></script>


    <link rel="stylesheet" href="/js/easyui1.5/themes/icon.css">
    <link rel="stylesheet" href="/js/easyui1.5/themes/default/easyui.css">
    <script src="/js/easyui1.5/jquery.easyui.min.js"></script>
</head>

<script type="text/javascript"  th:inline="none">


    $(function () {
        $('#add_div').dialog('close');
        getUser();
    })


    function getUser(){


        $("#dg").datagrid({

            url:'/user/queryUser',
            striped:true,
            pagination:true,
            toolbar:"#gongneng",
            pageList:[1,2,3,4,5,7,10],
            pageSize:5,
            checkOnSelect:false,
            columns:[[
                {field:'xxx',title:'xx',width:100,checkbox:true,align:'center'},
                {field:'img',title:'头像',width:100,align:'center',
                    formatter:function(value,row,index){
                        return "<img width='77px' height='77px' src='"+value+"'>";
                    }
                },
                {field:'userAccout',title:'账号',width:100,align:'center'},
                {field:'nickName',title:'昵称',width:100,align:'center' },
                {field:'userPwd',title:'密码',width:100,align:'center'},
                {field:'userSex',title:'性别',width:100,align:'center',
                    formatter:function(value,row,index){

                       if(value==1){
                           return "男";
                       }else if(value==2){

                           return "女";
                       }else {
                           return "";
                       }

                    }
                },
                {field:'grade',title:'等级',width:100,align:'center'},
                {field:'hy',title:'会员状态',width:100,align:'center',
                    formatter:function(value,row,index){

                        if(value==1){
                            return "普通用户";
                        }else if(value==2){

                            return "普通会员";
                        }else if(value==3){
                            return "超级会员";
                        }else{
                            return "";
                        }
                    }},
                   {field:'status',title:'当前状态',width:100,align:'center',
                    formatter:function(value,row,index){

                        if(value==1){
                            return "正常";
                        }else {
                            return "已禁言";
                        }

                    }},
                {field:'addTime',title:'创建时间',width:100,align:'center'},
                {field:'phone',title:'手机号',width:100,align:'center',},
                {field:'xsxs',title:'操作',width:300,align:'center',
                    formatter:function(value,row,index){
                    var content = " <a href='JavaScript:delUser("+row.userId+")' >删除</a> ";
                        content += " <a href='JavaScript:queryUserById("+row.userId+")' >修改</a> ";
                        content += " <a href='JavaScript:updateUserState("+row.userId+")' >加入黑名单</a> ";

                        if(row.status==1){
                            content += " <a href='JavaScript:updateStatus("+row.userId+",0)' >禁言</a> ";
                        }else {
                            content += " <a href='JavaScript:updateStatus("+row.userId+",1)' >允许发言</a> ";
                        }

                        return content;
                }
                }
            ]]

        })
    }

    function updateStatus(id,status) {


        $.ajax({
            url: "/user/updateStatus",
            type: "POST",
            data: {userId:id,status:status},
            success: function (pageUtil) {
                getUser();
            },
            error: function () {
                alert("失败了");
            }
        })
    }

    function updateUserState(id) {

        $.ajax({
            url:"/user/updateUserState",
            type:"POST",
            data:{userId:id,state:0},
            success:function(pageUtil){
                getUser();
            },
            error:function(){
                alert("失败了");
            }
        })

    }

    function queryUserById(id) {

        $("#user-from").form("clear");

        $.ajax({
            url:"/user/queryUserById",
            type:"POST",
            data:{id:id},
            success:function(result){
                $('#add_div').dialog('open');

                if(result.state==1){
                    $("#state1").prop("checked",true);
                }else if(result.state==2){
                    $("#state2").prop("checked",true);
                }

               $("#testimg").attr("src","/js/images/"+result.img);
                $("#user-from").form('load',result);

            },
            error:function(){
                alert("失败了");
            }
        })
    }

    function setEvent(){

                var rows = $("#dg").datagrid("getChecked");
                var ids = "";
                $.each(rows,function(index,obj){
                    ids += ids==""?obj.userId:","+obj.userId;
                });

                if(ids!=null&&ids!=""){
                    delUser(ids);
                }


    }

    function delUser(ids){


        $.ajax({
            url:"/user/delUser",
            type:"POST",
            data:{ids:ids},
            success:function(pageUtil){
                getUser();

                $.messager.show({
                    title:'消息',
                    msg:'删除成功!!',
                    timeout:5000,
                    showType:'slide'
                });

            },
            error:function(){
                alert("失败了");
            }
        });


    }


    function updateUser(){

        var formData = new FormData($("form")[0]);

        $.ajax({

            url:"/user/updateUser",
            data:formData,
            type:"POST",
            cache: false,
            contentType: false,
            processData: false,
            success:function(result){
                $('#add_div').dialog('close');
                getUser();
            },
            error:function(){
                alert("出错啦!!!");
            }
        })

    }


    function poi() {
        var rows = $("#dg").datagrid("getChecked");
        var ids = "";
        $.each(rows,function(index,obj){
            ids += ids==""?obj.userId:","+obj.userId;
        });

        if(ids!=null&&ids!=""){

            location.href="/poi/queryUserPoi?ids="+ids;
        }

    }
</script>


<body>

      <div id="gongneng">
          <a href="JavaScript:void(0)" onclick="setEvent()" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"></a>

          <a href="JavaScript:void(0)" onclick="poi()" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">导出</a>

      </div>
      <table id="dg" fit="true"></table>
      <div id="add_div"  class="easyui-dialog" title="新增用户" style="width:600px;height:300px"
           data-options="iconCls:'icon-save',resizable:true,modal:true,
			buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					 updateUser()//调用另一个方法
				}
			},{
				text:'关闭',
				iconCls:'icon-no',
				handler:function(){
				   $('#add_div').dialog('close');
				}
			}]">

          <form id="user-from">
                     <input type="hidden" name="userId">

              <table>
                  <tr>
                      <td>账号</td>
                      <td><input name="userAccout"></td>
                  </tr>
                  <tr>
                      <td>密码</td>
                      <td><input name="userPwd"></td>
                  </tr>
                  <tr>
                      <td>昵称</td>
                      <td><input name="nickName"></td>
                  </tr>
                  <tr>
                      <td>年龄</td>
                      <td><input name="userAge"></td>
                  </tr>
                  <tr>
                      <td>手机号</td>
                      <td><input name="phone"></td>
                  </tr>
                  <tr>
                      <td>会员状态</td>
                      <td>
                          <input type="radio"  id="state1" name="state" value="1">普通用户
                          <input type="radio" id="state2"  name="state" value="2">超级用户
                      </td>
                  </tr>
                  <tr>
                      <td>图片</td>
                      <td>
                          <input type="file" name="fileName">
                          <input type="hidden" name="img">
                          <img width="100px" id="testimg">
                      </td>
                  </tr>
              </table>
          </form>


      </div>

</body>
</html>